﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace BlunderBrain.VisualStudio.AviSynth
{
    public class AviSynthScriptEnvironment : BlunderBrain.AviSynth.Interop.ScriptEnvironment
    {
        public static string[] Keywords {get; private set;}
        
        static AviSynthScriptEnvironment()
        {
            Keywords = new string[] {"clip", "int", "float", "string", "bool", "val", "true", "false", "yes", "no", "function", "global", "return", "try", "catch", "last", "current_frame", "__END__" };
        }

        public IEnumerable<AviSynthFunction> InternalFunctions { get; private set; }
        public IEnumerable<AviSynthFunction> PluginFunctions { get; private set; }
        public IList<AviSynthFunction> ScriptFunctions { get; private set; }

        public AviSynthScriptEnvironment()
        {
            ScriptFunctions = new List<AviSynthFunction>();
            
            List<AviSynthFunction> internalFuncList = new List<AviSynthFunction>();
            foreach (string functionName in this.GetInternalFunctions())
                internalFuncList.Add(new AviSynthFunction(functionName, this.GetFunctionParameterSpec(functionName)));

            internalFuncList.Sort(new Comparison<AviSynthFunction>(delegate(AviSynthFunction x, AviSynthFunction y)
            {
                return string.Compare(x.Name, y.Name);
            }));

            InternalFunctions = internalFuncList;

            RefreshPluginFunctions();
        }

        public void RefreshPluginFunctions()
        {
            List<AviSynthFunction> pluginFuncList = new List<AviSynthFunction>();
            foreach (string functionName in this.GetPluginFunctions())
                pluginFuncList.Add(new AviSynthFunction(functionName, this.GetFunctionParameterSpec(functionName)));

            pluginFuncList.Sort(new Comparison<AviSynthFunction>(delegate(AviSynthFunction x, AviSynthFunction y)
            {
                return string.Compare(x.Name, y.Name);
            }));

            PluginFunctions = pluginFuncList;
        }

    }
}
